<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>


	<script>
		function nextLevel(obj) {
			var y="";
			$.ajax({
				url:"/tbItemCatController/nextLevel",
				type:"get",
				datatype:"json",
				data:{pid:obj},
				async:true,
				success:function(result){
					y+='<input type="hidden" value="'+result[0].parentId+'" id="parenId" name="parenId" />';
					for (var i=0;i<result.length;i++){
						y+='<tr ><td><input value="'+result[i].id+'" name="box" type="checkbox" ></td><td>'+result[i].id+'</td><td>'+result[i].name+'</td><td>'+result[i].typeId+'</td><td class="text-center"><button type="button" value="'+result[i].id+'" class="btn bg-olive btn-xs" onclick="nextLevel(this.value)">查询下级</button><button type="button" value="'+result[i].id+'" class="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate(this.value)" data-target="#updateModal" >修改</button></td></tr>';
					}
					$("#TbItemCatTable").html(y);
					heads(result[0].parentId);

				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}

		function heads(pid) {
			var z="";
			var x = "";
			$.ajax({
				url:"/tbItemCatController/heads",
				type:"get",
				datatype:"json",
				data:{id:pid},
				async:true,
				success:function(result){
					z+='<li><a href="javascript:getData()" >顶级分类列表</a></li>';
					for(var i = result.length; i>0 ;i--){
						z+='<li><a href="javascript:nextLevel('+result[i-1].id+')" >'+result[i-1].name+'</a></li>';
						x+=''+result[i-1].name+'>>';
					}
					//主页面
					$("#header").html(z);
					//增加页面
					$("#TheSuperior").html(x);
					//修改页面
					$("#TheSuperiorUpdate").html(x);
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}
		
		//删除
		function deleteTbItemCat() {
			var boxs =document.getElementsByName("box");
			var ids ="";
			for (var i = 0; i < boxs.length; i++) {
				if(boxs[i].checked){
					ids +=",'"+boxs[i].value+"'";
				}
			}
			ids =ids.substring(1);
			$.ajax({
				url:"/tbItemCatController/deleteTbItemCat",
				type:"get",
				datatype:"json",
				data:{ids:ids},
				async:true,
				success:function(result){
					alert(result.member+","+result.content+","+JSON.stringify(result.data));
					window.location.reload();
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})

		}

		//增加
		function saveTbTypeTemplate() {
			$.ajax({
				url:"/tbItemCatController/saveTbItemCat",
				type:"get",
				datatype:"json",
				data:{parentId:$("#parenId").val(),name:$("[name=name]").val(),typeId:$("[name=typeId]").val()},
				async:true,
				success:function(result){
					alert(result.member+","+result.content+","+JSON.stringify(result.data));
					window.location.reload();
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}

		//回显
		function toUpdate(obj) {
			$.ajax({
				url:"/tbItemCatController/toUpdateTbItemCat",
				type:"get",
				datatype:"json",
				data:{id:obj},
				async:true,
				success:function(result){
					$("#tid").val(result.id);
					$("#name").val(result.name);
					var se =$("#typeId").find("option");
					for (var x = 0; x < se.length; x++){
						if ($(se[x]).val()==result.typeId){
							$(se[x]).attr("selected","selected");
						}
					}
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}


		//确认修改
		function updateTbItemCats() {
			$.ajax({
				url:"/tbItemCatController/updateTbItemCats",
				type:"get",
				datatype:"json",
				data:{id:$("#tid").val(),parentId:$("[name=parenId]").val(),name:$("#name").val(),typeId:$("#typeId").val()},
				async:true,
				success:function(result){
					alert(result.member+","+result.content+","+JSON.stringify(result.data));
					window.location.reload();
				},
				error:function(result){
					alert("系统异常，请联系管理员！！！");
				}
			})
		}


		function getData() {
			location.href="/tbItemCatController/getFindAll";
		}

	</script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="header">
                        		<li>
		                        	<a href="javascript:getData()" >顶级分类列表</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteTbItemCat()" ><i class="fa fa-trash-o" ></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新"  onclick="getData()"><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="TbItemCatTable">
								  <th:block th:each="tb:${tbItemCats}">
									  <tr >
										  <td><input th:value="${tb.id}" name="box" type="checkbox" ></td>
										  <td th:text="${tb.id}"></td>
										  <td th:text="${tb.name}"></td>
										  <td th:text="${tb.typeId}"></td>
										  <td class="text-center">
											  <button type="button" th:value="${tb.id}" class="btn bg-olive btn-xs" onclick="nextLevel(this.value)">查询下级</button>
											  <button type="button" th:value="${tb.id}" class="btn bg-olive btn-xs" onclick="toUpdate(this.value)" data-toggle="modal" data-target="#updateModal" >修改</button>
										  </td>
									  </tr>
									  <input type="hidden" th:value="${tb.parentId}" name="parenId" id="parenId"/>
								  </th:block>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id = "TheSuperior">
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" name="name" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
		      			<select class="form-control" name="typeId">
							<option value="-1">请选择</option>
							<option th:each="tbType:${tbTypeTemplates}" th:value="${tbType.id}">[[${tbType.name}]]</option>
						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="saveTbTypeTemplate()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>



  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品分类修改</h3>
			  </div>
			  <div class="modal-body">
				  <input type="hidden" id="tid" />
				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td id="TheSuperiorUpdate">
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input id="name"  class="form-control" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <select  class="form-control" id="typeId">
								  <option value="-1">请选择</option>
								  <option th:each="tbType:${tbTypeTemplates}" th:value="${tbType.id}">[[${tbType.name}]]</option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" onclick="updateTbItemCats()" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>
</html>